#import "CTEncryptTool.h"
#define UPLOAD_Parameter_AES_KEY @"3410974a5261c3ce836161842803fb18"
#define BLOCK_SIZE 32
@implementation CTEncryptTool
+ (NSString *)getUploadParameterAESKey:(NSString *)key{
    NSString * md5 = key.jk_md5String;
    return [md5 substringToIndex:16];
}
+ (char)chr:(int)a{
    Byte target = (Byte)(a & 0xFF);
    return (char) target;
}
+ (NSData *)PKCS7Encode:(NSUInteger)count{
    int amountToPad = BLOCK_SIZE - (count % BLOCK_SIZE);
    if (amountToPad == 0){
        amountToPad = BLOCK_SIZE;
    }
    char padChr = [self chr:amountToPad];
    NSMutableString *tmp = [[NSMutableString alloc] init];
    for (int index = 0; index < amountToPad; index++){
        [tmp appendFormat:@"%c",padChr];
    }
    return [tmp dataUsingEncoding:NSUTF8StringEncoding];
}
+ (NSString *)convertStringToHexStr:(NSString *)str {
    if (!str || [str length] == 0) {
        return @"";
    }
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[data length]];
    [data enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop) {
        unsigned char *dataBytes = (unsigned char*)bytes;
        for (NSInteger i = 0; i < byteRange.length; i++) {
            NSString *hexStr = [NSString stringWithFormat:@"%x", (dataBytes[i]) & 0xff];
            if ([hexStr length] == 2) {
                [string appendString:hexStr];
            } else {
                [string appendFormat:@"0%@", hexStr];
            }
        }
    }];
    return string;
}
+ (NSString *)encryptInfo:(NSString *)info {
    NSString *base_zuid = [[[CTUserManager ct_getZuid] dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0];
    NSString *aes_Key = [NSString stringWithFormat:@"%@%@",UPLOAD_Parameter_AES_KEY,base_zuid];
    NSString *final_key = [self getUploadParameterAESKey:aes_Key];
    NSMutableData *resultMutableData = [[NSMutableData alloc] initWithData:[info dataUsingEncoding:NSUTF8StringEncoding]];
    NSData *pkcs7data = [self PKCS7Encode:resultMutableData.length];
    [resultMutableData appendData:pkcs7data];
    NSData *resultData = [resultMutableData AESEncryptWithKey:final_key];
    NSString *resultString = [resultData jk_base64EncodedString];
    return [self convertStringToHexStr:resultString];
}
+ (NSString *)decryptInfo:(NSString *)encrypted {
    NSString *base_zuid = [[[CTUserManager ct_getZuid] dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0];
    NSString *aes_Key = [NSString stringWithFormat:@"%@%@",UPLOAD_Parameter_AES_KEY,base_zuid];
    NSString *final_key = [self getUploadParameterAESKey:aes_Key];
    NSString *resultString = nil;
    resultString = [self convertToStringFromHexStr:encrypted];
    NSData *resultBaseData = [[NSData alloc] initWithBase64EncodedString:resultString options:0];
    NSData *aesResultData = [resultBaseData AESDecryptWithKey:final_key];
    NSData *pkcsData = [self PKCS7Decode:aesResultData];
    return [[NSString alloc] initWithData:pkcsData encoding:NSUTF8StringEncoding];
}
+ (NSData *)PKCS7Decode:(NSData *)data{
    Byte *dataByte = (Byte *)[data bytes];
    int pad = dataByte[data.length - 1];
    if (pad > 32 || pad < 1) {
        pad = 0;
    }
    NSData *newData = [data subdataWithRange:NSMakeRange(0, data.length - pad)];
    return newData;
}
+ (NSString *)convertToStringFromHexStr:(NSString *)str {
    if (!str || [str length] == 0) {
        return nil;
    }
    NSMutableData *hexData = [[NSMutableData alloc] initWithCapacity:8];
    NSRange range;
    if ([str length] % 2 == 0) {
        range = NSMakeRange(0, 2);
    } else {
        range = NSMakeRange(0, 1);
    }
    for (NSInteger i = range.location; i < [str length]; i += 2) {
        unsigned int anInt;
        NSString *hexCharStr = [str substringWithRange:range];
        NSScanner *scanner = [[NSScanner alloc] initWithString:hexCharStr];
        [scanner scanHexInt:&anInt];
        NSData *entity = [[NSData alloc] initWithBytes:&anInt length:1];
        [hexData appendData:entity];
        range.location += range.length;
        range.length = 2;
    }
    NSString *string = [[NSString alloc]initWithData:hexData encoding:NSUTF8StringEncoding];
    return string;
}
@end
